package starComponent.elements;


import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;

import data.visu.utils.Numbers;

import starComponent.E_PossibleBranches;
import starComponent.Listeners.ListenerFlyingNameLabel;
import starComponent.controls.ObserverThumb;
import tuio.components.LabelTouch;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class FlyingNameLabel extends LabelTouch implements ObserverThumb{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6974747826012867165L;

	public static final int MARGIN_LABEL=20;

	public static final int SIDE_LABEL = 100;

	protected A_StarBranch branch;

	protected String value = "";
	


	public FlyingNameLabel(A_StarBranch branch){
		this.branch = branch;
		ListenerFlyingNameLabel listener = new ListenerFlyingNameLabel(this);
		this.addMouseListener(listener);
		this.addMouseMotionListener(listener);
	}

	/**
	 * Constructeur par recopie, sans les listeners
	 * @param flyingNameLabel le FlyingNameLabel a recopier
	 */
	public FlyingNameLabel(FlyingNameLabel flyingNameLabel) {
		this.branch = flyingNameLabel.getBranch();
	}

	@Override
	public void paintComponent(Graphics g){

		Graphics2D g2d = (Graphics2D)g;

		FontMetrics metrics = g.getFontMetrics();
		int textLength = metrics.stringWidth(getText());
		int valueLength = metrics.stringWidth(value);

		//si orientation du popup selon utilisateur
		if(branch.getStar().getControler().isCollaborative()){
			double angle = -branch.getAngle()-Math.PI/2;
			g2d.rotate(angle, this.getWidth()/2, this.getHeight()/2);
		}


		g.drawString(getText(), (this.getWidth()-textLength)/2, (this.getHeight()/2));

		g.drawString(value, (this.getWidth()-valueLength)/2, this.getHeight()/2+metrics.getHeight());
	}
	
	protected double[] calculateLabelPosition(){
		String text=branch.getName();
		double xLabel = branch.getxEnd();
		double yLabel = branch.getyEnd();
		double angle = branch.getAngle();
		if(angle>=0){
			if(angle>15*Math.PI/8 || angle <Math.PI/8){
				xLabel = xLabel+2*MARGIN_LABEL;
				//yLabel = yLabel-MARGIN_LABEL-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=Math.PI/4){
				xLabel = xLabel+3*MARGIN_LABEL;
				yLabel = yLabel-text.length()*7;
			}
			else if(angle<=2*Math.PI/4){
				xLabel = xLabel-text.length()*7/2-3*MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=3*Math.PI/4){
				xLabel = xLabel-text.length()*7-MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=4*Math.PI/4){
				xLabel = xLabel-text.length()*7-MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=5*Math.PI/4){
				xLabel = xLabel - text.length()*7/2;
				yLabel = yLabel+MARGIN_LABEL;
			}
			else if(angle<=6*Math.PI/4){
				xLabel = xLabel - text.length()*7/2+2*MARGIN_LABEL;
				yLabel = yLabel-StarComponent.LABEL_HEIGHT/2;
			}
			else if(angle<=7*Math.PI/4){
				xLabel = xLabel -text.length()*7/2+ 2*MARGIN_LABEL;
				yLabel = yLabel - MARGIN_LABEL-StarComponent.LABEL_HEIGHT;
			}
			else if(angle<=8*Math.PI/4){
				xLabel = xLabel + MARGIN_LABEL;
				yLabel = yLabel- MARGIN_LABEL-StarComponent.LABEL_HEIGHT;
			}
			else System.err.println("l'angle doit etre inferieur a 2PI");
		}
		else System.err.println("l'angle doit etre superieur a 0");
		
		double[] res = new double[2];
		res[0] = xLabel;
		res[1] = yLabel;
		return res;
	}
	
	protected void calculateValue(){
		String text=branch.getName();
		this.setText(text);

		value = "";
		int branchType = branch.getBranchType().getType();
		if(branch instanceof StarBranchSlider){
			if(branchType==E_PossibleBranches.QUANTITATIVE_SLIDER){
				value = Double.toString(Numbers.roundDouble(((StarBranchSlider) branch).getValue(),2));
			}
			else if(branchType==E_PossibleBranches.QUALITATIVE_SLIDER){
				value = branch.getBranchType().getLabels()[(int) ((StarBranchSlider)branch).getValue()].getString();
			}
		}
		else if(branch instanceof StarBranchBiSlider){
			if(branchType==E_PossibleBranches.QUANTITATIVE_BISLIDER){
			value = Double.toString(Numbers.roundDouble(((StarBranchBiSlider) branch).getMinValue(),2))+
					" -> "+
					Double.toString(Numbers.roundDouble(((StarBranchBiSlider) branch).getMaxValue(),2));
			}
			else if(branchType==E_PossibleBranches.QUALITATIVE_BISLIDER){
				value = branch.getBranchType().getLabels()[(int) ((StarBranchBiSlider)branch).getMinValue()].getString()+
						" -> "+
						branch.getBranchType().getLabels()[(int) ((StarBranchBiSlider)branch).getMaxValue()].getString();
			}
		}
	}

	public void updateText(){
		double[]posLabel = calculateLabelPosition();
		calculateValue();

		int starWidth = (int) branch.getStar().getSize().getWidth();
		int starHeight = (int) branch.getStar().getSize().getHeight();

		if(posLabel[0]<0)posLabel[0]=0;
		if(posLabel[1]<0)posLabel[1]=0;
		if(posLabel[0]+SIDE_LABEL>starWidth)posLabel[0] = starWidth-SIDE_LABEL;
		if(posLabel[1]+SIDE_LABEL>starHeight)posLabel[1] = starHeight-SIDE_LABEL;

		this.setBounds((int)posLabel[0], (int)posLabel[1], SIDE_LABEL, SIDE_LABEL);
	}

	public A_StarBranch getBranch() {
		return branch;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getValue() {
		return value;
	}

	public FlyingNameLabel copy() {
		return new FlyingNameLabel(this);
	}


	@Override
	public void update(A_Thumb thumb) {
		updateText();
	}


}
